// @ts-ignore
import WindowInfo = cube.windows.WindowInfo;

const showWin = (win: string) => {
  cube.windows.getWindowByName(win).then((v: WindowInfo) => {
    if (v.show) {
      cube.windows.hide(v.id)
    } else {
      cube.windows.show(v.id)
    }
  }).catch(() => {
    cube.windows.obtainDeclaredWindow(win)
  })
}

cube.os.tray.setMenu(
  async (e, ...args) => {
    if (e === 'item-click') {
      switch (args[0]?.id) {
        case 'showMain':
          showWin('main')
          break
        case 'matchDetail':
          cube.windows.obtainDeclaredWindow('recentMatch').then(value => {
            if (value!==undefined){
              cube.windows.show(value.id)
            }
          })
          break
        case 'queryMatch':
          cube.windows.obtainDeclaredWindow('queryMatch')
          break
        case 'quit':
          cube.extensions.terminate()
          break
      }
    }else {
      showWin('main')
    }
  },
  [
    {type: 'normal', label: '显示助手', id: 'showMain'},
    {type: 'normal', label: '对局详情', id: 'matchDetail'},
    {type: 'normal', label: '查询战绩', id: 'queryMatch'},
    {type: 'normal', label: '退出软件', id: 'quit'},
  ],
  'Frank',
  ''
);
